<?php

namespace App\Http\Controllers;

use App\Exports\ExampleExport;
use App\Imports\JobImport;
use App\Jobs\ProcessTest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Redis;
use Maatwebsite\Excel\Facades\Excel;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class TestController extends Controller
{
    /**
     * 导出excel
     *
     * @param Request $request
     * @return void
     * @param
     * @author liangl
     */
    public function exportExcel(Request $request)
    {
        $exampleData = [
            [
                'no' => 1,
                'name' => '张三',
                'score' => 98,
            ]
        ];
        return Excel::download(new ExampleExport($exampleData), 'example.xlsx');
    }

    /**
     * 发送邮件
     *
     * @param Request $request
     * @return void
     * @param
     * @author liangl
     */
    public function sendMail(Request $request)
    {
        //send email
        // $exampleEmail = '1024651075@qq.com';
        // Mail::to($request->input('email'))->send(new Password($code));
        Mail::raw('测试测试', function ($message) {
            $message->to('1024651075@qq.com');
            $message->subject('开工大吉geework');
        });

        return $this->success();
    }

    /**
     * 生成二维码
     *
     * @param Request $request
     * @return void
     * @param
     * @author liangl
     */
    public function qrcode(Request $request)
    {
        // $codeSvg = QrCode::size(100)->generate('Hello,LaravelAcademy!');
        $codeSvg = QrCode::size(100)->generate('https://www.simplesoftware.io');
        echo $codeSvg;
        //return $this->success(['qrcode'=>$codeSvg]);
    }

    /**
     * 队列处理
     *
     * @param Request $request
     * @return void
     * @author liangl
     */
    public function doQueue(Request $request)
    {
        // dispatch(new ProcessTest);
        //Queue::push(new ProcessTest);
        //delay exec
        //later($delay, $job, $data = '', $queue = null);
        Queue::later(5, new ProcessTest);
        echo 'done....';
        Log::channel('v1api')->info('queue api done');
        return;
    }

    /**
     * Undocumented function
     *
     * @param Request $request
     * @param integer $num
     * @return void
     * @author liangl
     * @DateTime 2020-08-18
     */
    public function doProduce(Request $request, int $num)
    {
        $redis = Redis::connection('queue');
    }

    /**
     * 加入频率限制的api
     *
     * @author liangl
     * @DateTime 2020-08-24
     */
    public function ratelimit(Request $request)
    {
        return $this->success(['msg' => 'welcome!!']);
    }

    /**
     * 测试导入excel
     *
     * @author liangl
     * @DateTime 2020-08-24
     */
    public function test()
    {
        $fileName = dirname(dirname(dirname(__DIR__))) . '/' . 'resources' . '/' . 'data' . '/' . 'job.xlsx';

        $ret = Excel::import((new JobImport), $fileName);

        print_r($ret);
        exit;
    }
}
